﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using MobileBroadbandUserBlog.MBLM.Core;
using Rhino.Mocks;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;

namespace MobileBroadbandLoggingMonitor.FastTests
{
    [TestFixture]
    public class ChangeStatisticsTests
    {
        private readonly MockRepository _mockRepository = new MockRepository();
        private IDateTimeContainer _dateTimeContainer;

        [SetUp] 
        public void SetUp()
        {
            _dateTimeContainer = _mockRepository.StrictMock<IDateTimeContainer>();
        }


        [Test]
        public void TestAddingToLogAndThenClear()
        {
            DateTime[] dates = new DateTime[] {
            DateTime.Now.AddSeconds(-30),
            DateTime.Now.AddSeconds(-15),
            DateTime.Now.AddSeconds(-10)
            };

            DateTime frozenNow = DateTime.Now;
            using (_mockRepository.Record())
            {
                Expect.Call(_dateTimeContainer.GetNow()).Return(frozenNow).Repeat.Times(6);
            }

            using (_mockRepository.Playback())
            {
                ChangeStatistics changeStatistics = new ChangeStatistics(_dateTimeContainer);
                foreach (DateTime date in dates)
                {
                    changeStatistics.AddToStatistics(date);
                }

                int expected = 3;
                Assert.AreEqual(expected, changeStatistics.ChangesPerMinute);
                Assert.AreEqual(expected * 60, changeStatistics.ChangesPerHour);
                Assert.AreEqual(expected * 60 * 8, changeStatistics.ChangesPerWorkday);

                changeStatistics.ResetStatistics();
                Assert.AreEqual(0, changeStatistics.ChangesPerMinute);

            }
        }

        [Test]
        public void TestAddingToLogWithOneAdjustment()
        {
            DateTime[] dates = new DateTime[] {
            DateTime.Now.AddSeconds(-70),
            DateTime.Now.AddSeconds(-15),
            DateTime.Now.AddSeconds(-10)
            };

            DateTime frozenNow = DateTime.Now;
            using (_mockRepository.Record())
            {
                Expect.Call(_dateTimeContainer.GetNow()).Return(frozenNow).Repeat.Times(4);
            }

            using (_mockRepository.Playback())
            {
                ChangeStatistics changeStatistics = new ChangeStatistics(_dateTimeContainer);
                foreach (DateTime date in dates)
                {
                    changeStatistics.AddToStatistics(date);
                }

                int expected = 2;
                Assert.AreEqual(expected, changeStatistics.ChangesPerMinute);
                Assert.AreEqual(expected * 60, changeStatistics.ChangesPerHour);
                Assert.AreEqual(expected * 60 * 8, changeStatistics.ChangesPerWorkday);
            }
        }

    
    }
}
